﻿using System;
using System.IO;
using System.Windows.Forms;
using AMS.Common;

namespace AMS.ComponentModel
{
    public class FrmMainModel
    {
        private FrmMainModel()
        {
        }

        static FrmMainModel instance;

        #region Public Properties
        /// <summary>
        /// 主窗体实例
        /// </summary>
        public static FrmMainModel Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FrmMainModel();
                }

                return instance;
            }
        }

        /// <summary>
        /// 获取或设置主窗体
        /// </summary>
        public Form MainForm { get; set; }
        /// <summary>
        /// 主窗体句柄
        /// </summary>
        public IntPtr MainFormHandle
        {
            get
            {
                if (MainForm != null)
                {
                    return MainForm.Handle;
                }
                return IntPtr.Zero;
            }
        }

        /// <summary>
        /// 进度条
        /// </summary>
        public object ProgressBar { get; set; }
        /// <summary>
        /// 菜单
        /// </summary>
        public object MenuBar { get; set; }
        /// <summary>
        /// 状态条
        /// </summary>
        public object StatusBar { get; set; }
        #endregion

        #region 显示窗体的API
        /// <summary>
        /// 检查窗体是否存在
        /// </summary>
        /// <param name="formType"></param>
        /// <returns></returns>
        public Form GetExistingForm(Type formType)
        {
            foreach (Form frm in MainForm.MdiChildren)
            {
                if (frm.GetType() == formType)
                {
                    return frm;
                }
            }

            return null;
        }
        /// <summary>
        /// 根据窗体的TAG检测子窗体是否存在
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        public Form GetExistingForm(string tag)
        {
            foreach (Form frm in MainForm.MdiChildren)
            {
                if (frm.Tag == tag)
                {
                    return frm;
                }
            }

            return null;
        }

        /// <summary>
        /// 根据tag检测是否存在工作窗体，如果存在，则显示，否则创建窗体并显示
        /// </summary>
        /// <param name="formType"></param>
        /// <param name="tag"></param>
        /// <returns></returns>
        public Form CheckAndShowForm(Type formType, string tag)
        {
            Form frm = GetExistingForm(tag);
            if (frm == null)
            {
                frm = ShowWorkForm(formType);
                frm.Tag = tag;
            }
            else
            {
                frm.Show();
                frm.BringToFront();
            }

            return frm;
        }

        /// <summary>
        /// 显示窗体
        /// </summary>
        /// <param name="typeName"></param>
        /// <returns></returns>
        public Form ShowWorkForm(string typeName)
        {
            return ShowWorkForm(typeName, WorkFormMode.WorkForm);
        }

        /// <summary>
        /// 显示窗体
        /// </summary>
        /// <param name="typeName"></param>
        /// <param name="formMode"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public Form ShowWorkForm(string typeName, WorkFormMode formMode)
        {
            try
            {
                Type type = Type.GetType(typeName);
                return ShowWorkForm(type, formMode);
            }
            catch (FileNotFoundException ex)
            {
                throw new FormLoadException(ex.Message);
            }
            catch (FileLoadException ex)
            {
                throw new FormLoadException(ex.Message);
            }
            catch (BadImageFormatException ex)
            {
                throw new FormLoadException(ex.Message);
            }

            throw new FormLoadException(LocaleFramework.GetString("@ErrorInFormLoad"));
        }
        /// <summary>
        /// 显示窗体
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public Form ShowWorkForm(Type formType)
        {
            return ShowWorkForm(formType, WorkFormMode.WorkForm);
        }

        /// <summary>
        /// 显示窗体
        /// </summary>
        /// <param name="formType"></param>
        /// <param name="formMode"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public Form ShowWorkForm(Type formType, WorkFormMode formMode)
        {
            Form formInstance = Activator.CreateInstance(formType) as Form;
            if (formInstance == null)
            {
                if (formInstance is IDisposable)
                {
                    ((IDisposable)formInstance).Dispose();
                }

                throw new ArgumentException(LocaleFramework.GetString("@ArgumentIsNotFormType"));
            }

            if (formInstance is IWorkForm)
            {
                ((IWorkForm)formInstance).Init();
            }

            try
            {
                ShowWorkForm(formInstance, formMode);
            }
            catch (Exception)
            {
                if (formInstance != null)
                {
                    formInstance.Dispose();
                }

                return null;
            }

            return formInstance;
        }
        /// <summary>
        /// 显示窗体
        /// </summary>
        /// <param name="form"></param>
        /// <param name="formMode"></param>
        public void ShowWorkForm(Form form, WorkFormMode formMode)
        {
            if (form == null)
            {
                throw new ArgumentNullException("form");
            }

            switch (formMode)
            {
                case WorkFormMode.WorkForm:
                    form.MdiParent = MainForm;
                    form.FormBorderStyle = FormBorderStyle.None;
                    form.StartPosition = FormStartPosition.Manual;
                    form.Show();
                    form.BringToFront();
                    ////form.WindowState = FormWindowState.Maximized;
                    form.Dock = DockStyle.Fill;
                    break;

                case WorkFormMode.Dialog:
                    form.StartPosition = FormStartPosition.CenterParent;
                    form.ShowInTaskbar = false;
                    form.ShowDialog(MainForm);
                    break;

                case WorkFormMode.Form:
                    form.Show();
                    form.BringToFront();
                    break;

                default:
                    throw new NotImplementedException(LocaleFramework.GetFormattedString("@NoImplimationForTheValue", formMode.ToString()));
            }
        }

        /// <summary>
        /// 显示已经存在的工作窗体
        /// </summary>
        /// <param name="workForm"></param>
        public void ShowExistingWorkForm(Form workForm)
        {
            workForm.Show();
            workForm.BringToFront();
        }
        #endregion
    }

    /// <summary>
    /// 窗体显示模式
    /// </summary>
    public enum WorkFormMode
    {
        /// <summary>
        /// 工作窗体
        /// </summary>
        WorkForm,
        /// <summary>
        /// 对话框
        /// </summary>
        Dialog,
        /// <summary>
        /// 普通的窗体
        /// </summary>
        Form
    }
}
